package com.tensquare.base.service;

import com.tensquare.base.dao.LabelDao;
import com.tensquare.base.pojo.Label;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import util.IdWorker;

import javax.persistence.criteria.Predicate;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;


/**
 * 标签业务逻辑类
 *
 * @Auther tang
 */
@Service
@Transactional//事务
public class LabelService {

    @Autowired
    private LabelDao labelDao;

    @Autowired
    private IdWorker idWorker;

    /**
     * 查看全部标签
     *
     * @return
     */
    public List<Label> findAll() {
        return labelDao.findAll();
    }

    /**
     * 根据ID查询标签
     *
     * @param id
     * @return
     */
    public Label findById(String id) {
        return labelDao.findById(id).get();
    }

    /**
     * 保存标签
     *
     * @param label
     */
    public void save(Label label) {
        label.setId(idWorker.nextId() + "");
        labelDao.save(label);
    }

    /**
     * 修改标签
     *
     * @param label
     */
    public void update(Label label) {
        labelDao.save(label);
    }

    /**
     * 删除标签
     *
     * @param id
     */
    public void deleteById(String id) {
        labelDao.deleteById(id);
    }

    /**
     * @param label
     * @return
     */
    public List<Label> findSearch(Label label) {
        /**
         * root 根对象，也就是要把条件封装到哪个对象中，where类名=label.getid
         * criteriaQuery 封装的都是查询关键字，比如groupby，orderby
         * criteriaBuilder 用来封装条件对象的
         */
        return labelDao.findAll((root, criteriaQuery, criteriaBuilder) -> {
            List<Predicate> predicates = new ArrayList<>();
            if (label.getLabelname() != null && !"".equals(label.getLabelname())) {
                Predicate predicate = criteriaBuilder.like(root.get("labelname").as(String.class), "%" + label.getLabelname() + "%");//where labelname like "%法外狂徒张三%"
                predicates.add(predicate);
            }
            if (label.getState() != null && !"".equals(label.getState())) {
                Predicate predicate = criteriaBuilder.like(root.get("state").as(String.class), label.getState());//where labelname like "%法外狂徒张三%"
                predicates.add(predicate);
            }

            Class<String> stringClass = String.class;
            //where labelname like "%法外狂徒张三%" and state = 1
            Predicate[] predicatearr = new Predicate[predicates.size()];

            return criteriaBuilder.and(predicates.toArray(predicatearr));
        });
    }

    /**
     * 分页条件查询
     * @param label
     * @param page
     * @param size
     * @return
     */
    public Page findSearch(Label label, int page, int size) {
        return labelDao.findAll((root, criteriaQuery, criteriaBuilder) -> {
            List<Predicate> predicates = new ArrayList<>();
            if (label.getLabelname() != null && !"".equals(label.getLabelname())) {
                Predicate predicate = criteriaBuilder.like(root.get("labelname").as(String.class), "%" + label.getLabelname() + "%");//where labelname like "%法外狂徒张三%"
                predicates.add(predicate);
            }
            if (label.getState() != null && !"".equals(label.getState())) {
                Predicate predicate = criteriaBuilder.like(root.get("state").as(String.class), label.getState());//where labelname like "%法外狂徒张三%"
                predicates.add(predicate);
            }

            Class<String> stringClass = String.class;
            //where labelname like "%法外狂徒张三%" and state = 1
            Predicate[] predicatearr = new Predicate[predicates.size()];

            return criteriaBuilder.and(predicates.toArray(predicatearr));
        }, PageRequest.of(page - 1, size));
    }
}
